package com.chen.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.common.Result;
import com.chen.mapper.SignMapper;
import com.chen.mapper.UserMapper;
import com.chen.pojo.Sign;
import com.chen.pojo.User;
import com.chen.service.Impl.SignServiceImpl;
import com.chen.service.SignService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @author chen

 */
@RestController
@Api(value = "SignController", tags = "签到")
@RequestMapping(value = "/sign")
//@RequestMapping(path = {"/sign"})
public class SignController {
    @Resource
    private HttpServletRequest request;

    @Resource
    private SignMapper signMapper;

    @Resource
    private SignServiceImpl signService;

    @Resource
    private UserMapper userMapper;


    @ApiOperation("插入")
    @PostMapping("/insert")
    public Result insertSign(@RequestBody Sign sign) {
        sign.setTime(new Date());

        return Result.success(signMapper.insert(sign));
    }

    @ApiOperation("查询某一个")
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable("id") int id) {
        return Result.success(signService.selectById(id));
    }

    @ApiOperation("查询全部")
    @GetMapping("/findAll")
    public Result findAll() {

        return Result.success(signMapper.selectList(null));

    }

    @ApiOperation("查询是否过期")
    @GetMapping("/selectIsSigned/{username}")
    public Result selectIsSigned(@PathVariable("username") String username) {

        Sign sign = new Sign();
        Sign res = signService.selectIsSigned(username);
        String userId = request.getHeader("X-ID");
        System.out.println(Integer.parseInt(userId));
        if (ObjectUtil.isEmpty(res)) {
            userMapper.addPoints(Integer.parseInt(userId));
            return Result.success(res);
        } else {

          /* userMapper.addPoints(userId);
            signService.save(sign);*/
            return Result.success("今天已经签到过了,请勿重复签到");
        }
    }


    //    分页查询
    @GetMapping("/page")
    public IPage<Sign> findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                                @RequestParam(defaultValue = "") String username,
                                @RequestParam(defaultValue = "") String time) {
        IPage<Sign> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Sign> queryWrqpper = new QueryWrapper<>();

        if (!"".equals(username)) {
            queryWrqpper.like("username", username);
        }
        if (!"".equals(time)) {
            queryWrqpper.like("time", time);
        }

        queryWrqpper.orderByDesc("id");
        IPage<Sign> userPage = signService.page(page, queryWrqpper);
        return userPage;


    }
}
